<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>拆分记录</title>
    <script type="text/javascript">
      var newSeparateWin;
      var editSeparateWin;
      var separateOrderId;
      Ext.onReady(function(){
        productSuitStore =  Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'ProductCBB',
          proxy : {
            type : 'ajax',
            url : '/product/getAllSuitProducts',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }//,
          //pageSize : pageSize,
          //autoLoad : true
        });
        
        var orderNumber = Ext.create('Ext.form.field.Text', {
          fieldLabel: '拆分单号',
          name: 'order_number'
        });

        //产品
        var productCBB = Ext.create('Ext.form.ComboBox', {
          labelWidth:35,
          store: productStore,
          fieldLabel: '产品',
          emptyText: '请选择...',
          valueField: 'id',
          displayField: 'name',
          name: 'product_id',
          loadingText: '搜索中...',
          pageSize: 10,
          anchor: '100%',
          minChars: 0,
          listConfig: {
            loadingText: 'Searching...',
            getInnerTpl: function() {
              return '<div style="float:left; width:200px" data-qtip="{name}">{name:ellipsis(18)}</div><div style="float:left; width:110px">{number}</div>{is_suit}';
            }
          }
        });

        var warehouse =  Ext.create('Ext.form.ComboBox', {
          fieldLabel: '拆分仓库',
          store: warehouseStore,
          valueField: 'id',
          displayField: 'name',
          name:'warehouse_id',
          loadingText: '搜索中...',
          //pageSize: 10,
          minChars: 0,
          editable:false
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '拆分日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          vtype: 'daterange',
          endDateField: 'end_date'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至',
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          vtype: 'daterange',
          startDateField: 'begin_date'
        });

        var submitButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          handler: function(){
            separateOrderDS.removeAll();
            /*separateOrderDS.getProxy().extraParams.order_number = orderNumber.getValue();
            separateOrderDS.getProxy().extraParams.warehouse_id = warehouse.getValue();
            separateOrderDS.getProxy().extraParams.product_id = productCBB.getValue();
            separateOrderDS.getProxy().extraParams.begin_date = beginDate.getRawValue();
            separateOrderDS.getProxy().extraParams.end_date = endDate.getRawValue();
            separateOrderDS.getProxy().extraParams.start = 0;
            separateOrderDS.load();*/
            
            separateOrderDS.getProxy().extraParams = queryFormPanel.getForm().getFieldValues();
            toolBar.moveFirst(); 
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 65,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [orderNumber]
                    }, {
                      columnWidth: .20,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [warehouse]
                    }, {
                      columnWidth: .40,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [productCBB]
                    }]
                },{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .18,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .20,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [endDate]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [submitButton]
                    }, {
                      columnWidth: .15,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //显示拆分操作时的窗口介面。
        //----------------------------------------------------------------------
        //拆分Grid Store
        var separateOrderDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'SeparateOrderModel',
          proxy : {
            type : 'ajax',
            url : '/separate_order/getAllSeparateOrders',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          },
          pageSize : pageSize,
          autoLoad : true
        });
        
        var toolBar = Ext.create('Ext.toolbar.Paging', {
          pageSize: pageSize,
          store: separateOrderDS,
          displayInfo: true,
          displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
          emptyMsg: "没有记录",
          items:["-",{
              text: '新增拆分订单',
              iconCls: 'addItem',
              //disabled:true,
              handler : function(){
                newSeparateWin = Ext.create('Ext.window.Window', {
                  layout: 'fit',
                  width: 815,
                  height: 495,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加拆分单',
                  autoLoad: {
                    url: "/separate_order/newSeparate",
                    scripts: true
                  }
                });
                newSeparateWin.on("destroy",function(){
                  separateOrderDS.load();
                });
                newSeparateWin.resizable = false;
                newSeparateWin.show();
              }
            },'-',{
              text: '编辑',
              iconCls: 'editItem',
              handler : function(){
                var records = separateOrderGrid.getSelectionModel().getSelection();
                if(records.length==0){
                  Ext.MessageBox.show({
                    title: '提示信息',
                    msg: "请先选中一条记录后，再编辑。",
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.WARNING
                  });
                }else{
                  separateOrderId = records[0].get('id');
                  if(records[0].get('auditing_status').indexOf("申请") < 0 && records[0].get('auditing_status').indexOf("通过") < 0 && records[0].get('deal_with_status') != "已撤消"){
                    editSeparateWin = Ext.create('Ext.window.Window', {
                      layout: 'fit',
                      width: 815,
                      height: 500,
                      closeAction: 'destroy',
                      plain: true,
                      modal: true,
                      constrain:true,
                      //modal: true,
                      title: '编辑拆分订单',
                      autoLoad: {
                        url: "/separate_order/editSeparate?id="+separateOrderId,
                        scripts: true,
                        callback: function(options, success, response, request)
                        {
                          if (response.responseText === "NoPrivilege"){
                            editSeparateWin.close();
                          }
                        }
                      }
                    });
                    editSeparateWin.on("destroy",function(){
                      separateOrderDS.load();
                    });
                    editSeparateWin.resizable = false;
                    editSeparateWin.show();
                  }else{
                    Ext.MessageBox.alert('提示信息','处于申请状态或已经通过审批的拆分订单，不能编辑。');
                  }
                }
              }
            },'-',{
              text: '撤消',
              iconCls: 'cancel',
              handler : function(){
                var records = separateOrderGrid.getSelectionModel().getSelection();
                if (records.length == 0) {
                  return false;
                }else{
                  if(records[0].get('deal_with_status') != '已撤消'){
                    var orderNumber = records[0].get('order_number')
                    separateOrderId = records[0].get('id');
                    Ext.MessageBox.confirm("提示","你确认要申请撤消订单号为： \""+orderNumber+"\" 的拆分订单吗？",function(btn){
                      if(btn=="yes"){
                        Ext.Ajax.request({
                          url : '/separate_order/applyToCancel?id=' + separateOrderId,
                          success : function (result, request) {
                            separateOrderDS.load();
                          },
                          failure : function (result, request){
                            Ext.MessageBox.show({
                              title: '消息',
                              msg: "通讯失败，请从新操作",
                              buttons: Ext.MessageBox.OK,
                              icon: Ext.MessageBox.WARNING
                            });
                          },
                          method : 'GET'
                        });
                      }else{
                        //alert("no");
                      }
                    });
                  }
                }
              }
            }]
        })
        
        //生成要订单的产品的Grid
        var separateOrderGrid = Ext.create('Ext.grid.Panel', {
          title:'拆分订单列表',
          store: separateOrderDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '拆分单号',
              dataIndex: 'order_number',
              width: 120
            },{
              header: '拆分仓库',
              dataIndex: 'warehouse_name',
              width: 100
            },{
              header: '货品名称',
              dataIndex: 'product_name',
              width: 200
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 50
            },{
              header: '操作员',
              dataIndex: 'employee_name',
              width: 70
            },{
              header: '部门',
              dataIndex: 'department_name',
              width: 70
            },{
              header: '拆分日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 80
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 50
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 170
            },{
              header: '订单处理状态',
              dataIndex: 'deal_with_status',
              width: 90,
              renderer: auditingStatusRender
            },{
              header: '产品ID',
              dataIndex: 'product_id',
              hidden:true,
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 170
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
              width: 115
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: 320,
          frame: true,
          tbar: toolBar
        });

        separateOrderGrid.on('itemdblclick', function(gridPanel, record,item,index,e,options){
          separateOrderId = separateOrderDS.getAt(index).get('id');
          var separateOrderNumber = separateOrderDS.getAt(index).get('order_number');
          //separateOrderDS.load();
          separateOrderDetailDS.removeAll();
          separateOrderDetailDS.load({params:{'separate_order_id': separateOrderId}});
          separateOrderDetailGrid.setTitle("拆分订单："+separateOrderNumber+"，拆分出的产品明细");
        });

        //--------------------------拆分订单详细，双击后可显示数据----------------
        //拆分订单Grid Store Detail
        var separateOrderDetailDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'AssemblyOrderDetailModel',
          proxy : {
            type : 'ajax',
            url : '/separate_order/getSeparateOrderDetailsById',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });
        
        //产品拆分成的产品的详细的Grid
        var separateOrderDetailGrid = Ext.create('Ext.grid.Panel', {
          title:'拆分单详细列表',
          store: separateOrderDetailDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 60
            },{
              header: '货品名称',
              dataIndex: 'product_name',
              width: 200
            },{
              header: '规格',
              dataIndex: 'product_spec',
              width: 80
            },{
              header: '数量',
              align: 'right',
              dataIndex: 'quantity',
              width: 80
            },{
              header: '套装或单品',
              dataIndex: 'is_suit',
              width: 70
            },{
              header: '备注',
              dataIndex: 'description',
              width: 150
            }],
          loadMask: true,
          width: screenWidth-165,
          height: dgHeight,
          frame: true
          //title: '进货品种列表',
          //plain:true,
          //plugins: checkColumn,
        });

        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: totalHeight,autoScroll:true,
          frame:false,          
          layout: {type: 'table',columns:1},          
          items:[queryFormPanel,separateOrderGrid,separateOrderDetailGrid]
        });
        workbenchPanel.render("separate_order_list");
        
      })
    </script>
  </head>
  <body>
    <div id="separate_order_list"></div>
  </body>
</html>
